﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Pantry.Commons;
using Pantry.Entity;
using Pantry.DataAccess;

namespace Pantry.Business
{
    public class RoleBus
    {
        private readonly RoleDao _roleDao;
        /// <summary>
        /// Initializes a new instance of the <see cref="RoleBus"/> class.
        /// </summary>
        public RoleBus()
        {
            _roleDao = new RoleDao();
        }
        /// <summary>
        /// Lists the role.
        /// </summary>
        /// <returns></returns>
        public DataTable ListRole()
        {
            try
            {
                return _roleDao.ListRoles();
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }
        }
        /// <summary>
        /// Roles the name by id.
        /// </summary>
        /// <param name="roleId">The role id.</param>
        /// <returns></returns>
        public string RoleNameById(int roleId)
        {
            try
            {
                return _roleDao.RoleNameById(roleId);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return string.Empty;
            }
        }
        /// <summary>
        /// Adds the role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        public bool AddRole(Role role)
        {
            try
            {
                var result = _roleDao.AddRole(role);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Lists the role by id.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <returns></returns>
        public DataTable ListRoleById(int organizationId)
        {
            try
            {
                return _roleDao.ListRoleById(organizationId);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }
        }
    }
}
